package dao;

import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import model.Customer;

public class SpringJdbcDaoSupportApp extends JdbcDaoSupport {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		SpringJdbcDaoSupportApp jdbcDao = (SpringJdbcDaoSupportApp) ctx.getBean("jdbcDao");
		// queryForList
		String sql = "select * from customer t";
		List reList = jdbcDao.queryListBySql(sql);
		if (reList.size() > 0) {
			for (int i = 0; i < reList.size(); i++) {
				String typename = ((Map) reList.get(i)) == null ? "" : ((Map) reList.get(i)).get("name").toString();
				System.out.println(typename);
			}
		}
		
		// queryForObject
		System.out.println(jdbcDao.getObject());
	}

	public List queryListBySql(String sql) {
		return this.getJdbcTemplate().queryForList(sql);
	}
	
	public Customer getObject() {
		String sql2 = "select * from customer t where cust_id = ?";
		Customer cust = (Customer)getJdbcTemplate().queryForObject(sql2, new Object[]{2}, new CustomerRowMapper());
		return cust;
	}

}